package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavors;
import com.itheima.domain.vo.DishVo;
import com.itheima.mapper.DishMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorsService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorsService dishFlavorsService;

    @Override
    public R<String> adminDishSave(DishVo dishVo) {
        int size = dishVo.getFlavors().size();
        if (size == 0) {
            return R.error("请添加口味");
        }
        Dish dish = new Dish();
        DishFlavors flavors = new DishFlavors();
        BeanUtils.copyProperties(dishVo, dish);
        // 菜品类型
        dish.setType(0);
        // 菜品排序
        dish.setSort(1L);
        Category category = categoryService.getById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        // 调用存储方法
        boolean resultAddDish = save(dish);


        ArrayList<DishFlavors> dishFlavorsArrayList = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            BeanUtils.copyProperties(dishVo.getFlavors().get(i), flavors);
            // 把新增菜品的id添加给口味id
            flavors.setId(dish.getId());
            dishFlavorsArrayList.add(flavors);
        }

        boolean resultAddDishFlavors = dishFlavorsService.saveBatch(dishFlavorsArrayList);

        if (resultAddDish && resultAddDishFlavors) {
            return R.success("添加成功");
        }
        return R.error("添加失败");

    }

    @Override
    public R<Page<Dish>> adminPageSelect(String name, Integer page, Integer pageSize) {
        // 构造分页构造器
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        // 构造条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        // 添加排序
        queryWrapper.orderByDesc(Dish::getSort);
        // 执行查询
        page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    @Override
    public R<DishVo> adminSelectDishById(String id) {

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getId, id);
        Dish dish = getOne(lqw);


        LambdaQueryWrapper<DishFlavors> lqwdf = new LambdaQueryWrapper<>();
        lqwdf.eq(DishFlavors::getId, id);
        List<DishFlavors> dishFlavors = dishFlavorsService.list(lqwdf);

        DishVo dishVo = new DishVo();
        BeanUtils.copyProperties(dish, dishVo);
        BeanUtils.copyProperties(dishFlavors, dishVo);
        return R.success(dishVo);
    }


    @Override
    public R<String> adminUpdateDish(DishVo dishVo) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVo, dish);
        boolean updateResult = updateById(dish);
        dishFlavorsService.removeById(dishVo.getId());
        ArrayList<DishFlavors> flavorsArrayList = dishVo.getFlavors();
        for (DishFlavors dishFlavors : flavorsArrayList) {
            dishFlavors.setId(dishVo.getId());
        }

        boolean resultSaveDishFlavors = dishFlavorsService.saveBatch(flavorsArrayList);

        if (updateResult && resultSaveDishFlavors) {
            return R.success("修改菜品成功");
        }
        return R.error("修改菜品失败");
    }

    @Override
    public R<String> adminDishDelete(List<String> ids) {
        boolean resultRemoveDish = removeByIds(ids);
        boolean resultRemoveDishFlavors = dishFlavorsService.removeByIds(ids);
        if (resultRemoveDish && resultRemoveDishFlavors) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    @Override
    public R<String> adminDishStatus(List<String> ids, Integer status) {
        ArrayList<Dish> dishArrayList = new ArrayList<>();
        for (String id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishArrayList.add(dish);
        }

        boolean resultUpdateDishList = updateBatchById(dishArrayList);
        if (resultUpdateDishList) {
            return R.success("操作成功");
        }

        return R.error("操作失败");
    }


    @Override
    public R<List<Dish>> adminCategoryList(String categoryId,String name ) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(categoryId), Dish::getCategoryId, categoryId)
                .like(StringUtils.isNotEmpty(name), Dish::getName, name);
        List<Dish> list = list(lambdaQueryWrapper);
        return R.success(list);
    }


    @Override
    public R<List<DishVo>> clientCategoryList(String categoryId,  String status ){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(categoryId),Dish::getCategoryId,categoryId)
                .eq(StringUtils.isNotEmpty(status),Dish::getStatus,status);
        List<Dish> dishList=list(lambdaQueryWrapper);

        List<String> idList = dishList.stream().map(Dish::getId).collect(Collectors.toList());
        //通过菜品id集合获取到与菜品关联的口味集合....一个菜品可以有多个口味
        List<DishFlavors> flavorsList = dishFlavorsService.listByIds(idList);
        Map<String,ArrayList<DishFlavors>> map=new HashMap<>();

        for (DishFlavors dishFlavors : flavorsList) {
            String id = dishFlavors.getId();
            ArrayList<DishFlavors> dishFlavorsArrayList=new ArrayList<>();
            boolean result = map.containsKey(id);
            if (result){
                ArrayList<DishFlavors> dishFlavors1 = map.get(id);
                dishFlavors1.add(dishFlavors);
                map.put(id, dishFlavors1);
                //跳出这次循环
                continue;
            }
            dishFlavorsArrayList.add(dishFlavors);
            map.put(id, dishFlavorsArrayList);
        }

        List<DishVo> dishVoList = dishList.stream().map(dish -> {
            DishVo dishVo = new DishVo();
            dishVo.setFlavors(map.get(dish.getId()));
            BeanUtils.copyProperties(dish, dishVo);
            return dishVo;
        }).collect(Collectors.toList());


        return R.success(dishVoList);
    }


}
